﻿using StudyEnglish.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace StudyEnglish.ViewModel
{
    public class DailyViewModel : INotifyPropertyChanged
    {
        private AppDataContext appDB;

        public DailyViewModel(string appConnectionString)
        {
            appDB = new AppDataContext(appConnectionString);
        }

        private int _countDayByUser;
        public int CountDayByUser
        {
            get { return _countDayByUser; }
            set
            {
                _countDayByUser = value;
                NotifyPropertyChanged("CountDayByUser");
            }
        }
        
        private int _countMaxDaily;
        public int CountMaxDaily
        {
            get { return _countMaxDaily; }
            set
            {
                _countMaxDaily = value;
                NotifyPropertyChanged("CountMaxDaily");
            }
        }
        
        private int _maxDaily;
        public int MaxDaily
        {
            get { return _maxDaily; }
            set
            {
                _maxDaily = value;
                NotifyPropertyChanged("MaxDaily");
            }
        }

        private AppDaily _dailyByUserID_VolID;
        public AppDaily DailyByUserID_VolID
        {
            get { return _dailyByUserID_VolID; }
            set
            {
                _dailyByUserID_VolID = value;
                NotifyPropertyChanged("DailyByUserID_VolID");
            }
        }

        public void saveChangeDB()
        {
            appDB.SubmitChanges();
        }

        public void GetCountDayByUser(int userID)
        {
            _countDayByUser = (from s in appDB.Daily
                        where s.UserID == userID && s.Day == 1
                        select s).Count();
        }

        public void AddDaily(AppDaily newDaily)
        {
            appDB.Daily.InsertOnSubmit(newDaily);
            appDB.SubmitChanges();

        }

        public void GetMaxDaily(int userID)
        {


            _maxDaily = (from s in appDB.Daily
                      where s.UserID == userID
                      select s).Max(u => u.Day);

        }

        public void GetCountMaxDaily(int userID, int maxDay)
        {
            _countMaxDaily = (from s in appDB.Daily
                               where s.UserID == userID && s.Day == maxDay
                        select s).Count();
        }

        public void UpdDaily(AppDaily newDaily)
        {
            // find a city to update
            GetDailyByUserID_VolID(newDaily.UserID, newDaily.VocabularyID);

            AppDaily old = DailyByUserID_VolID;

            old.Day = newDaily.Day;
            old.UserID = newDaily.UserID;
            old.VocabularyID = newDaily.VocabularyID;
            appDB.SubmitChanges();

        }

        public void GetDailyByUserID_VolID(int userID, int VolID)
        {
            _dailyByUserID_VolID = (from s in appDB.Daily
                                   where s.UserID == userID && s.VocabularyID == VolID
                               select s).FirstOrDefault();
        }
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }
}
